Download
FAQ
History
HomeHomeNext API
Search
Feedback
Divider

The Example JSP Pages

To illustrate JSP technology, this chapter rewrites each servlet in the Duke's Bookstore application introduced in The Example Servlets as a JSP page:

Table 12-1 Duke's Bookstore Example JSP Pages 
Function
JSP Pages
Enter the bookstore
bookstore.jsp
Create the bookstore banner
banner.jsp
Browse the books offered for sale
bookcatalog.jsp
Add a book to the shopping cart
bookcatalog.jsp and bookdetails.jsp
Get detailed information on a specific book
bookdetails.jsp
Display the shopping cart
bookshowcart.jsp
Remove one or more books from the shopping cart
bookshowcart.jsp
Buy the books in the shopping cart
bookcashier.jsp
Receive an acknowledgement for the purchase
bookreceipt.jsp

The data for the bookstore application is still maintained in a database. However, two changes are made to the database helper object database.BookDB:

The implementation of the database helper object follows. The bean has two instance variables: the current book and the data access object.

package database;
public class BookDB {
  private String bookId = "0";
  private BookDBAO database = null;

  public BookDB () throws Exception {
  }
  public void setBookId(String bookId) {
    this.bookId = bookId;
  }
  public void setDatabase(BookDAO database) {
    this.database = database;
  }
  public BookDetails getBookDetails() 
    throws Exception {
    return (BookDetails)database.getBookDetails(bookId);
  }
  ...
} 

This version of the Duke's Bookstore application is organized along the Model-View-Controller (MVC) architecture. The MVC architecture is a widely-used architectural approach for interactive applications that separates functionality among application objects so as to minimize the degree of coupling between the objects. To achieve this, it divides applications into three layers: model, view, and controller. Each layer handles specific tasks and has responsibilities to the other layers:


Note: When employed in a Web application, the MVC architecture is often referred to as a Model-2 architecture. The bookstore example discussed in the previous chapter, which intermixes presentation and business logic, follows what is known as a Model-1 architecture. The Model-2 architecture is the recommended approach to designing Web applications.


In addition, this version of the application uses several custom tags from the JavaServer Pages Standard Tag Library (JSTL) (see Chapter 13):

Custom tags are the preferred mechanism for performing a wide variety of dynamic processing tasks, including accessing databases, using enterprise services such as e-mail and directories, and flow control. In earlier versions of JSP technology, such tasks were performed with JavaBeans components in conjunction with scripting elements (discussed in Chapter 15). Though still available in JSP 2.0, scripting elements tend to make JSP pages more difficult to maintain because they mix presentation and logic, which is discouraged in page design. Custom tags are introduced in Using Custom Tags and described in detail in Chapter 14.

Finally, this version of the example contains an applet to generate a dynamic digital clock in the banner. See Including an Applet for a description of the JSP element that generates HTML for downloading the applet.

  1. The source code for the application is located in the <INSTALL>/j2eetutorial14/examples/web/bookstore2/ directory (see Building and Running the Examples). A sample bookstore2.war is provided in <INSTALL>/j2eetutorial14/examples/web/provided-wars/. To build, package, deploy, and run the example:
  2. Build and package the bookstore common files as described in Duke's Bookstore Examples.
  3. In a terminal window, go to <INSTALL>/j2eetutorial14/examples/web/bookstore2/.
  4. Run asant build. This target will spawn any necessary compilations and copy files to the <INSTALL>/j2eetutorial14/examples/web/bookstore2/build/ directory.
  5. Start the J2EE application server.
  6. Perform all the operations described in Accessing Databases from Web Applications.
  7. Start deploytool.
  8. Create a Web application called bookstore2 by running the New Web Application Wizard. Select FileRight ArrowNewRight ArrowWeb Application WAR.
  9. New Web Application Wizard
    1. Select the Create New Stand-Alone WAR Module radio button.
    2. Click Browse.
    3. In the file chooser, navigate to <INSTALL>/j2eetutorial14/examples/web/bookstore2/.
    4. In the File Name field, enter bookstore2.
    5. Click Choose Module File.
    6. In the WAR Display Name field, enter bookstore2.
    7. In the Context Root field, enter /bookstore2.
    8. Click Edit.
    9. In the Edit Contents dialog, navigate to <INSTALL>/j2eetutorial14/examples/web/web/bookstore2/build/. Select the JSP pages bookstore.jsp, bookdetails.jsp, bookcatalog.jsp, bookshowcart.jsp, bookcashier.jsp, bookordererror.jsp, bookreceipt.jsp, duke.books.gif, Dispatcher.class, DigitalClock.class and the database, listeners, and template directories and click Add.
    10. Move /WEB-INF/classes/DigitalClass.class to the root directory of the WAR. By default, deploytool packages all classes in /WEB-INF/classes/. Since DigitalClock.class is a client-side class, it must be stored in the root directory. To do this, simply drag DigitalClock.class from /WEB-INF/classes/ to the root directory in the pane labeled Contents of bookstore2.
    11. Add the shared bookstore library. Navigate to <INSTALL>/j2eetutorial14/examples/build/web/bookstore/dist/. Select bookstore.jar and Click Add.
    12. Click OK.
    13. Click Next.
    14. Select the Servlet radio button.
    15. Click Next.
    16. Select Dispatcher from the Servlet class combo box.
    17. Click Finish.
  10. Add the listener class listeners.ContextListener (described in Handling Servlet Life Cycle Events).
    1. Select the Event Listeners tab.
    2. Click Add.
    3. Select the listeners.ContextListener class from drop down field in the Event Listener Classes panel.
  11. Add the aliases.
    1. Select the Dispatcher web component.
    2. Select the Aliases tab.
    3. Click Add and then type /bookstore in the Aliases field. Repeat to add the aliases /bookcatalog, /bookdetails, /bookshowcart, /bookcashier, /bookordererror, and /bookreceipt.
  12. Add the JSTL resource bundle basename context parameter.
    1. Select the Context tab.
    2. Click Add.
    3. Enter javax.servlet.jsp.jstl.fmt.localizationContext for the Coded Parameter.
    4. Enter messages.BookstoreMessages for the Value.
  13. Set prelude and codas for all JSP pages.
    1. Select the JSP Properties tab.
    2. Click the Add button next to the Name list.
    3. Enter bookstore2.
    4. Click the Add button next to the URL Pattern list.
    5. Enter /*.jsp.
    6. Click the Edit button next to the Include Preludes list.
    7. Click Add.
    8. Enter /template/prelude.jspf.
    9. Click OK.
    10. Click the Edit button next to the Include Codas list.
    11. Click Add.
    12. Enter /template/coda.jspf.
    13. Click OK.
  14. Add a resource reference for the database.
    1. Select the Resource Refs tab.
    2. Click Add.
    3. Enter jdbc/BookDB in the Coded Name field.
    4. Accept the default type javax.sql.DataSource.
    5. Accept the default authorization Container.
    6. Accept the default selected Shareable.
    7. Enter jdbc/BookDB in the JNDI name field of the Deployment setting for jdbc/BookDB frame.
  15. Select FileRight ArrowSave.
  16. Deploy the application.
    1. Select ToolsRight ArrowDeploy.
    2. In the Connection Settings frame, enter the user name and password you specified when you installed the J2EE 1.4 Application Server.
    3. Click OK.
    4. A popup dialog will display the results of the deployment. Click Close.
  17. Open the bookstore URL http://localhost:1024/bookstore2/bookstore. Click on the Start Shopping link and you will see the screen in Figure 12-2:

  18. Duke's Bookstore Book Catalog

Figure 12-2 Book Catalog

See Troubleshooting for help with diagnosing common problems related to the database server. If the messages in your pages appear as strings of the form ??? Key ???, the likely cause is that you have not provided the correct resource bundle basename as a context parameter.

Divider
Download
FAQ
History
HomeHomeNext API
Search
Feedback
Divider

All of the material in The J2EE 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.